//封装定时器的函数
function tips(color,text){
  document.querySelector('.tips_content2').style.display="block"
      document.querySelector('.tips_content2').style.color=color
      document.querySelector('.tips_content2').innerHTML=text
      setTimeout(function(){
        document.querySelector('.tips_content2').style.display="none"
      },3000)
}

window.onload = function () {
  //验证token是否失效

  axios.defaults.headers.common["Authorization"] =
    localStorage.getItem("token");
  axios
    .post("http://120.24.80.83:3000/refresh")
    .then(function (response) {
      console.log(response.data.msg);
      if (response.data.msg == "登录失效") {
        //提示框
        tips("red","登录失效，请重新登录")
      }
    })
    .catch(function (erroe) {
      console.log;
    });

  //获取用户信息

  axios.defaults.headers.common["Authorization"] =
    localStorage.getItem("token");
  axios
    .get("http://120.24.80.83:3000/user/getUserInfo")
    .then(function (response) {
      console.log(response.data.msg);
      console.log(response.data.data);
      if (response.data.msg == "获取成功") {
        var username = response.data.data.username;
        console.log(username);
        document.querySelector(".username2").innerHTML = username;
        document.querySelector(".header_img2").src = response.data.data.avatar;
        document.querySelector(".money1").innerHTML = response.data.data.money;
        document.querySelector(".header_img3").src = response.data.data.avatar;
      }
    })
    .catch(function (error) {
      console.log(error);
    });
};

//更换头像
var file = document.querySelector(".file");
document.querySelector(".Queren").onclick = function () {
  const formData = new FormData();
  formData.append("avatar", file.files[0]);
  axios.defaults.headers.common["Authorization"] =
    localStorage.getItem("token");
  axios({
    url: "http://120.24.80.83:3000/user/uploadAvatar",
    method: "post",
    data: formData,
  })
    .then(function (response) {
      console.log(response);
      if (response.data.msg == "上传头像成功") {
         //提示框
         tips("green","上传成功")
        console.log(response.data.data.url);
        document.querySelector(".header_img3").src = response.data.data.url;
        document.querySelector(".header_img2").src = response.data.data.url;
      } else {
         //提示框
         tips("red","上传失败!")
      }
      console.log(response.data.msg);
    })
    .catch(function (error) {
      console.log(error);
    });
};

//修改昵称
var new_username = document.querySelector(".new_username").value;
var rename = document.querySelector(".rename");

rename.onclick = function () {
  console.log("hhjghj");
  console.log(document.querySelector(".new_username").value);
  axios.defaults.headers.common["Authorization"] =
    localStorage.getItem("token");
  axios
    .post("http://120.24.80.83:3000/user/changeUserName", {
      username: document.querySelector(".new_username").value,
    })
    .then(function (response) {
      console.log(response);
      if (response.data.msg == "更改成功") {
        document.querySelector(".username2").innerHTML =
          document.querySelector(".new_username").value;
           //提示框
        tips("green","修改成功")
      } else {
         //提示框
         tips("red","修改失败")
      }
    })
    .catch(function (error) {
      console.log(error);
    });
};

//修改密码
var change_pw = document.querySelector(".change_pw");
var chainge_pv_content = document.querySelector(".chainge_pv_content");
var pw_close = document.querySelector(".pw_close");
var Chainge_Quxaio = document.querySelector(".Chainge_Quxaio");
var Chainge_Queren = document.querySelector(".Chainge_Queren");
//修密码框显示
change_pw.onclick = function () {
  chainge_pv_content.style.display = "block";
};

//关闭修改框
pw_close.onclick = function () {
  chainge_pv_content.style.display = "none";
};
Chainge_Quxaio.onclick = function () {
  chainge_pv_content.style.display = "none";
};

document.querySelector(".Chainge_Queren").onclick = function () {
  if (
    document.querySelector(".new_pw_input").value !=
    document.querySelector(".new_pw_input2").value
  ) {
    axios.defaults.headers.common["Authorization"] =
      localStorage.getItem("token");
    axios
      .post("http://120.24.80.83:3000/user/changePass", {
        "oldPassword": document.querySelector(".new_pw_input2").value,
       " newPassword": document.querySelector(".new_pw_input").value,
      })
      .then(function (response) {
        if (response.data.msg == "修改密码成功") {
            chainge_pv_content.style.display = "none";
          console.log(response);
           //提示框
        tips("green","修改成功")
        }
      })
      .catch(function (error) {
        console.log(error);
      });
  } else {
     //提示框
     tips("red","新密码跟旧密码重复!")
  }
};


//充值
var recharge=document.querySelector('.recharge')

recharge.onclick = function(){
    if(document.querySelector('.money_input').value<200){
        axios.defaults.headers.common["Authorization"] =
        localStorage.getItem("token");
      axios
        .post("http://120.24.80.83:3000/user/chargeMoney", {
          "money": document.querySelector(".money_input").value,
         
        }).then(function(response){
            if(response.data.msg=="充值成功"){
                document.querySelector(".money1").innerHTML=response.data.data.money
                //提示框
     tips("red","充值成功")
            }
            console.log(response)
        }).catch(function(error){
            console.log(error)
        })
    }
    else{
     //提示框
     tips("red","充值额不能超过200!")
    
    }
}